package com.jingdianjichi.application.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.jingdianjichi.application.controller.convert.AuthRoleDTOConvert;
import com.jingdianjichi.application.controller.dto.AuthRoleDTO;
import com.jingdianjichi.auth.domain.bo.AuthRoleBO;
import com.jingdianjichi.auth.domain.service.AuthRoleDomainService;
import com.jingdianjichi.auth.entity.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * ClassName:AuthRoleController
 * Package:com.jingdianjichi.application.controller
 * Description:
 *
 * @Author fst
 * @Create 2024/10/10 14:54
 * @Version 1.0
 */
@RequestMapping("/role/")
@Slf4j
@RestController
public class AuthRoleController
{
    @Resource
    private  AuthRoleDomainService authRoleDomainService;

    @PostMapping("add")
    public Result<Boolean> add(@RequestBody AuthRoleDTO authRoleDTO)
    {
        try {
            if (log.isInfoEnabled()) {
                log.info("AuthRoleController.add.dto:{}", JSON.toJSONString(authRoleDTO));
            }
            Preconditions.checkNotNull(authRoleDTO.getRoleName(),"角色姓名不能为空");
            Preconditions.checkNotNull(authRoleDTO.getRoleKey(),"roleKey不能为空");

            AuthRoleBO authRoleBO= AuthRoleDTOConvert.INSTANDE.convertDTOTOBORole(authRoleDTO);

            authRoleDomainService.add(authRoleBO);

            return Result.ok(true);
        } catch (Exception e) {
            log.error("AuthRoleController.add.error:{}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        }
    }

    @PostMapping("update")
    public Result<Boolean> update(@RequestBody AuthRoleDTO authRoleDTO)
    {
        try {
            if (log.isInfoEnabled()) {
                log.info("AuthRoleController.update.dto:{}", JSON.toJSONString(authRoleDTO));
            }
            Preconditions.checkNotNull(authRoleDTO.getId(),"id不能为空");

            AuthRoleBO authRoleBO= AuthRoleDTOConvert.INSTANDE.convertDTOTOBORole(authRoleDTO);

            authRoleDomainService.update(authRoleBO);

            return Result.ok(true);
        } catch (Exception e) {
            log.error("AuthRoleController.update.error:{}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        }
    }

    @PostMapping("delete")
    public Result<Boolean> delete(@RequestBody AuthRoleDTO authRoleDTO)
    {
        try {
            if (log.isInfoEnabled()) {
                log.info("AuthRoleController.delete.dto:{}", JSON.toJSONString(authRoleDTO));
            }
            Preconditions.checkNotNull(authRoleDTO.getId(),"id不能为空");


            AuthRoleBO authRoleBO= AuthRoleDTOConvert.INSTANDE.convertDTOTOBORole(authRoleDTO);

            authRoleDomainService.delete(authRoleBO);

            return Result.ok(true);
        } catch (Exception e) {
            log.error("AuthRoleController.delete.error:{}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        }
    }

}
